package org.eclipse.gittab.player.internal.midi;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.ShortMessage;

import org.eclipse.gittab.gitTabDsl.GuitarSound;
import org.eclipse.gittab.player.GitTabPlayer.EffectSounds;

public class MidiUtil {

	public static MidiMessage createInstrumentMessage(int instrument,
			int channel) {

		return createMessage(ShortMessage.PROGRAM_CHANGE, channel, instrument,
				0);
	}

	public static MidiMessage createOnMessage(int note, int channel,
			int velocity) {

		return createMessage(ShortMessage.NOTE_ON, channel, note, velocity);
	}

	public static MidiMessage createOffMessage(int note, int channel) {
		return createMessage(ShortMessage.NOTE_OFF, channel, note, 0);
	}

	public static MidiMessage createMessage(int msg, int channel, int data1,
			int data2) {

		ShortMessage message = new ShortMessage();
		try {
			message.setMessage(msg, channel, data1, data2);
		} catch (InvalidMidiDataException e) {
			e.printStackTrace();
		}

		return message;
	}

	/**
	 * Converts a {@link GuitarSound} to a MIDI event number.
	 * 
	 * @param guitar
	 * @return
	 */
	public static int guitarToMidi(GuitarSound guitar) {

		switch (guitar) {
		case CLEAN:
			return 27;
		case DISTORTION:
			return 30;
		case OVERDRIVE:
			return 29;
		case JAZZ_ELECTRIC:
			return 26;
		case NYLON_ACOUSTIC:
			return 24;
		case STEEL_ACOUSTIC:
			return 25;
		default: // not possible
			return 27;
		}
	}

	/**
	 * Converts a {@link SpecialGuitarSounds} to a MIDI event number.
	 * 
	 * @param guitar
	 * @return
	 */
	public static int effectToMidi(EffectSounds effect) {

		switch (effect) {
		case HARMONICS:
			return 31;
		case MUTED:
			return 28;
		default: // not possible
			return 28;
		}
	}
}
